use "/homeKRTK/health_shocks/data_for_main_regs_event_study_20231203.dta", clear
drop if control==1
append using "/homeKRTK/health_shocks/additional_data_for_all_regs_controls_nod_20240422.dta"
append using "/homeKRTK/health_shocks/additional_data_for_IV_and_placebo_regs_20240416.dta"

*keep if control == 1 | (HS==1 & ABS==0)

cap drop treated
gen treated=1-control

forval i=1/8 {
	local j=`i'+8
	rename mean_fq`i'_w w_q`j'
	replace mean_fq`i'_FEj_imp =. if flag_missingFEj_fq`i'==1
	rename mean_fq`i'_FEj_imp FE_q`j'


}
forval i=1/8 {
	local j=8-`i'
	rename mean_lq`i'_w w_q`j'
	replace mean_lq`i'_FEj_imp =. if flag_missingFEj_lq`i'==1
	rename mean_lq`i'_FEj_imp FE_q`j'
}


* reshape *
capture drop FEj_t4 FEj_t5 FEj_t6 FEj_t7 FEj_t8 FEj_t9 FEj_t10 FEj_t11 FEj_t12 
capture drop lnw_restr_t4 lnw_restr_t5 lnw_restr_t6 lnw_restr_t7 lnw_restr_t8 lnw_restr_t9 lnw_restr_t10 lnw_restr_t11 lnw_restr_t12 

reshape long w_q FE_q, i(anon) j(relative_time)

cap drop date
gen date=t+(relative_time-8)*3
replace date=t+d+(relative_time-9)*3 if relative_time>=8

rename ev ev_orig

rename t t_orig
rename date t


gen ev=floor((t-1)/12)+2003
merge 1:1 anon t using "/homeProspSSD/Admin3/admin3_alap.dta", nogen keep(master match) keepusing(vallazon1 wtip1 wh1 feor1_h2)
replace wh1=. if wh1>40
rename vallazon1 vallazon
merge m:1 vallazon ev using "/homeKRTK/health_shocks/firmquality.dta", nogen keep(master match)
merge 1:1 anon t using "/homeProspSSD/Admin3/admin3_eu_fekvo.dta", nogen keepusing(korhazi_nap)
merge 1:1 anon t using "/homeProspSSD/Admin3/admin3_eu_jaro.dta", nogen keepusing(jaro_db)
merge 1:1 anon t using "/homeProspSSD/Admin3/admin3_eu_hazio.dta", nogen keepusing(hsz_eset)
merge 1:1 anon t using "/homeKRTK/nok_munka/Mental_health/admin3_passziv_ho.dta", nogen keep(master match) keepusing(passziv_ho)

replace passziv_ho=0 if passziv_ho==.
foreach var of varlist korhazi_nap jaro_db hsz_eset {
	replace `var'=0 if `var'==. & ev>=2009
}

by anon, sort: egen full_d=min(d)
drop if full_d==.


rename t date
rename t_orig t

by anon, sort: egen full_t=min(t)


gen hosp_relevant1_=korhazi_nap if date>=full_t & date<=full_t+1
by anon, sort: egen hosp_relevant1=total(hosp_relevant1_)
drop hosp_relevant1_

gen jaro_relevant1_=jaro_db if date>=full_t & date<=full_t+1
by anon, sort: egen jaro_relevant1=total(jaro_relevant1_)

drop jaro_relevant1_

gen gp_relevant1_=hsz_eset if date>=full_t & date<=full_t+1
by anon, sort: egen gp_relevant1=total(gp_relevant1_)
drop gp_relevant1_

gen sl_relevant1_=passziv_ho if date>=full_t & date<=full_t+1
by anon, sort: egen sl_relevant1=total(sl_relevant1_)
drop sl_relevant1_

drop if d==.
drop if d>6

*indicator of minor accident
cap drop minor
gen minor=1
replace minor=0 if sl_relevant1>0 | hosp_relevant1>=5 
cap drop minor2
gen minor2=1
replace minor2=0 if sl_relevant1>0 

keep anon minor2
duplicates drop 

sort anon

save "/homeKRTK/health_shocks/minor2.dta", replace

use "/homeKRTK/health_shocks/data_for_main_regs_event_study_20231203.dta", clear
drop if control==1
append using "/homeKRTK/health_shocks/additional_data_for_all_regs_controls_nod_20240422.dta"
append using "/homeKRTK/health_shocks/additional_data_for_IV_and_placebo_regs_20240416.dta"

replace treated=1 if d==2

gen NONEMP = 1 if ABS==1
	recode NONEMP (.=0) if ABS==0
	
set seed 12345

keep if d<=6

sort anon

merge m:1 anon using "/homeKRTK/health_shocks/minor2.dta"

gen whitecoll = 1 if occupation>=1 & occupation<=4
	recode whitecoll (.=0) if occupation>=5 & occupation<=7
gen bluecoll = 1 if whitecoll==0
	recode bluecoll (.=0)
	
gen ACC = HS
gen group = 1 if ACC==1 & d>=2 & minor2==0
replace group = 2 if ACC==1 & d==1 & minor2==0
replace group = 3 if ACC == 0 

gen manufacturing =1 if teaor1_h1==4
	recode manufacturing (.=0)
gen trade = 1 if teaor1_h1==7
	recode trade (.=0)

table group if ev>=2012, c(count anon)

table group  if ev>=2012, c(mean ferfi mean kor mean bluecoll mean manufacturing mean trade)
*table group, c(mean FEi mean lnw1_restr_l4 mean FEj_l1 mean FO_pre mean size_pre)
table group  if ev>=2012, c(mean FEi mean mean_lq1_w_wins mean mean_lq1_FEj_imp mean FO_pre mean size_pre)

sort anon t
drop _merge
merge 1:1 anon t using "/homeKRTK/health_shocks/IVdata_accidents_bnoSext2_SL5_2022Sept26.dta", keepusing(log_health_12m fekvo_ft_m1 jaro_ft_ossz_m1 tbtam_ossz_m1)
keep if _m==3
table group if ev>=2012, c(mean log_health_12m mean fekvo_ft_m1 mean jaro_ft_ossz_m1 mean tbtam_ossz_m1)

drop _merge
gen st=tmerge_t
merge 1:1 anon st using "/homeKRTK/health_shocks/bnoSext_code_list.dta"
drop if _m==2
drop _m

gen BP=.
replace 	BP = 	7	if 	BNOS03	==	1
replace 	BP = 	7	if 	BNOS13	==	1
replace 	BP = 	7	if 	BNOS23	==	1
replace 	BP = 	7	if 	BNOS33	==	1
replace 	BP = 	1	if 	BNOS40	==	1
replace 	BP = 	1	if 	BNOS41	==	1
replace 	BP = 	1	if 	BNOS42	==	1
replace 	BP = 	1	if 	BNOS43	==	1
replace 	BP = 	1	if 	BNOS44	==	1
replace 	BP = 	1	if 	BNOS45	==	1
replace 	BP = 	1	if 	BNOS46	==	1
replace 	BP = 	1	if 	BNOS49	==	1
replace 	BP = 	2	if 	BNOS50	==	1
replace 	BP = 	2	if 	BNOS51	==	1
replace 	BP = 	2	if 	BNOS52	==	1
replace 	BP = 	2	if 	BNOS53	==	1
replace 	BP = 	2	if 	BNOS54	==	1
replace 	BP = 	2	if 	BNOS55	==	1
replace 	BP = 	2	if 	BNOS56	==	1
replace 	BP = 	2	if 	BNOS59	==	1
replace 	BP = 	3	if 	BNOS60	==	1
replace 	BP = 	3	if 	BNOS61	==	1
replace 	BP = 	3	if 	BNOS62	==	1
replace 	BP = 	3	if 	BNOS63	==	1
replace 	BP = 	3	if 	BNOS64	==	1
replace 	BP = 	3	if 	BNOS65	==	1
replace 	BP = 	3	if 	BNOS66	==	1
replace 	BP = 	3	if 	BNOS69	==	1
replace 	BP = 	4	if 	BNOS70	==	1
replace 	BP = 	4	if 	BNOS71	==	1
replace 	BP = 	4	if 	BNOS72	==	1
replace 	BP = 	4	if 	BNOS73	==	1
replace 	BP = 	4	if 	BNOS74	==	1
replace 	BP = 	4	if 	BNOS75	==	1
replace 	BP = 	4	if 	BNOS76	==	1
replace 	BP = 	4	if 	BNOS79	==	1
replace 	BP = 	5	if 	BNOS80	==	1
replace 	BP = 	5	if 	BNOS81	==	1
replace 	BP = 	5	if 	BNOS82	==	1
replace 	BP = 	5	if 	BNOS83	==	1
replace 	BP = 	5	if 	BNOS84	==	1
replace 	BP = 	5	if 	BNOS85	==	1
replace 	BP = 	5	if 	BNOS86	==	1
replace 	BP = 	5	if 	BNOS89	==	1
replace 	BP = 	6	if 	BNOS90	==	1
replace 	BP = 	6	if 	BNOS91	==	1
replace 	BP = 	6	if 	BNOS92	==	1
replace 	BP = 	6	if 	BNOS93	==	1
replace 	BP = 	6	if 	BNOS94	==	1
replace 	BP = 	6	if 	BNOS95	==	1
replace 	BP = 	6	if 	BNOS96	==	1
replace 	BP = 	6	if 	BNOS99	==	1

gen acc_type=.
replace 	acc_type=	4	if 	BNOS03	==	1
replace 	acc_type=	4	if 	BNOS13	==	1
replace 	acc_type=	4	if 	BNOS23	==	1
replace 	acc_type=	4	if 	BNOS33	==	1
replace 	acc_type=	1	if 	BNOS40	==	1
replace 	acc_type=	2	if 	BNOS41	==	1
replace 	acc_type=	3	if 	BNOS42	==	1
replace 	acc_type=	4	if 	BNOS43	==	1
replace 	acc_type=	1	if 	BNOS44	==	1
replace 	acc_type=	1	if 	BNOS45	==	1
replace 	acc_type=	1	if 	BNOS46	==	1
replace 	acc_type=	1	if 	BNOS49	==	1
replace 	acc_type=	1	if 	BNOS50	==	1
replace 	acc_type=	2	if 	BNOS51	==	1
replace 	acc_type=	3	if 	BNOS52	==	1
replace 	acc_type=	4	if 	BNOS53	==	1
replace 	acc_type=	1	if 	BNOS54	==	1
replace 	acc_type=	1	if 	BNOS55	==	1
replace 	acc_type=	1	if 	BNOS56	==	1
replace 	acc_type=	1	if 	BNOS59	==	1
replace 	acc_type=	1	if 	BNOS60	==	1
replace 	acc_type=	2	if 	BNOS61	==	1
replace 	acc_type=	3	if 	BNOS62	==	1
replace 	acc_type=	4	if 	BNOS63	==	1
replace 	acc_type=	1	if 	BNOS64	==	1
replace 	acc_type=	1	if 	BNOS65	==	1
replace 	acc_type=	1	if 	BNOS66	==	1
replace 	acc_type=	1	if 	BNOS69	==	1
replace 	acc_type=	1	if 	BNOS70	==	1
replace 	acc_type=	2	if 	BNOS71	==	1
replace 	acc_type=	3	if 	BNOS72	==	1
replace 	acc_type=	4	if 	BNOS73	==	1
replace 	acc_type=	1	if 	BNOS74	==	1
replace 	acc_type=	1	if 	BNOS75	==	1
replace 	acc_type=	1	if 	BNOS76	==	1
replace 	acc_type=	1	if 	BNOS79	==	1
replace 	acc_type=	1	if 	BNOS80	==	1
replace 	acc_type=	2	if 	BNOS81	==	1
replace 	acc_type=	3	if 	BNOS82	==	1
replace 	acc_type=	4	if 	BNOS83	==	1
replace 	acc_type=	1	if 	BNOS84	==	1
replace 	acc_type=	1	if 	BNOS85	==	1
replace 	acc_type=	1	if 	BNOS86	==	1
replace 	acc_type=	1	if 	BNOS89	==	1
replace 	acc_type=	1	if 	BNOS90	==	1
replace 	acc_type=	2	if 	BNOS91	==	1
replace 	acc_type=	3	if 	BNOS92	==	1
replace 	acc_type=	4	if 	BNOS93	==	1
replace 	acc_type=	1	if 	BNOS94	==	1
replace 	acc_type=	1	if 	BNOS95	==	1
replace 	acc_type=	1	if 	BNOS96	==	1
replace 	acc_type=	1	if 	BNOS99	==	1

recode acc_ (2=1) (4=2)
recode acc_ (1=2)

label define acc_ 2 "wound/dislocation/sprain" 3 "fracture"
lab val acc_ acc_

label define BP 1 "shoulder/upper-arm" 2 "elbow/forearm" 3 "hand/fingers/wrist" 4 "hip/thigh" 5 "knee/lower-leg" 6 "ankle/foot/toes" 7 "head/neck/thorax/spine" 
lab val BP BP

tab BP acc if d>=2 & minor2==0 & ev>=2012
	
tab BP, gen(BP_)
tab acc_type, gen(acc_type_)

tab BP acc_type if d>=2 & bluecoll==1 & minor2==0 & ev>=2012
tab BP acc_type if d>=2 & whitecoll==1 & minor2==0 & ev>=2012
